# Copyright 2022 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//bazel:yacl.bzl", "yacl_cc_library", "yacl_cc_test")

# core
yacl_cc_library(
    name = "entropy_source",
    visibility = ["//visibility:public"],
    deps = [
        ":urandom_factory",
    ] + select({
        "@platforms//cpu:x86_64": [
            ":intel_factory",
        ],
        "//conditions:default": [],
    }),
)

# private spi
yacl_cc_library(
    name = "spi",
    hdrs = [
        "entropy_source.h",
    ],
    visibility = ["//visibility:private"],
    deps = [
        "//yacl:secparam",
        "//yacl/utils/spi",
    ],
)

# private urandom drbg impl
yacl_cc_library(
    name = "urandom_factory",
    srcs = [
        "urandom_factory.cc",
    ],
    hdrs = [
        "urandom_factory.h",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":spi",
    ],
    alwayslink = 1,
)

# private intel drbg impl
yacl_cc_library(
    name = "intel_factory",
    srcs = [
        "intel_factory.cc",
    ],
    hdrs = [
        "intel_factory.h",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":spi",
        "@com_github_google_cpu_features//:cpu_features",
    ],
    alwayslink = 1,
)

yacl_cc_test(
    name = "factory_test",
    srcs = [
        "factory_test.cc",
    ],
    deps = [
        ":entropy_source",
    ],
)
